ROOT_DIR    = $(shell pwd)
NAMESPACE   = "default"
DEPLOY_NAME = "template-single"
DOCKER_NAME = "template-single"
ENV = local
CONFIG_DIR = config
#BINARY_NAME = $(shell basename `pwd`)
BINARY_NAME = main
BINARY_DIR = bin
BINARY_CONFIG = $(BINARY_DIR)/$(CONFIG_DIR)
VERSION = v1.6
NOW = $(shell date +"%Y%m%d%H%M%S")
HTTP_PORT=0
DOCKER_TAG_NAME=$(shell basename `pwd`):$(ENV)

#ldargs
LDflgs="-X 'gitee.com/igolang/pkg/boot.buildTime=$(NOW)' -X 'gitee.com/igolang/pkg/boot.version=$(VERSION)'"

.PHONY: buildBase
buildBase:
	rm -rf $(ROOT_DIR)/$(BINARY_DIR)
	mkdir -p $(ROOT_DIR)/$(BINARY_CONFIG)
	cp -rf $(ROOT_DIR)/$(CONFIG_DIR)/$(ENV)/* $(ROOT_DIR)/$(BINARY_CONFIG)
	go mod tidy -v

.PHONY: build
build: buildBase
	go build -tags=jsoniter -ldflags $(LDflgs) -o $(ROOT_DIR)/$(BINARY_DIR)/$(BINARY_NAME) -v $(ROOT_DIR)/main.go

.PHONY: build.windows
build.windows: buildBase
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -tags=jsoniter -ldflags $(LDflgs) -o $(ROOT_DIR)/$(BINARY_DIR)/$(BINARY_NAME).exe -v $(ROOT_DIR)/main.go

.PHONY: build.linux
build.linux: buildBase
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags=jsoniter -ldflags $(LDflgs) -o $(ROOT_DIR)/$(BINARY_DIR)/$(BINARY_NAME) -v $(ROOT_DIR)/main.go

.PHONY: build.docker
build.docker: build.linux
	cp $(ROOT_DIR)/docker-entrypoint.sh $(ROOT_DIR)/$(BINARY_DIR)/docker-entrypoint.sh
	docker build . -t $(DOCKER_TAG_NAME)

.PHONY: run
run:
	go run -tags=jsoniter -ldflags $(LDflgs)  main.go http -p $(HTTP_PORT) --xcfg.path=$(ROOT_DIR)/config/$(ENV)

.PHONY: gen
gen:
	go run -tags=jsoniter main.go gen -h

.PHONY: gen.dao
gen.dao:
	go run -tags=jsoniter main.go gen -d --xcfg.path=$(ROOT_DIR)/config/$(ENV)

.PHONY: demo
demo:
	go run -tags=jsoniter main.go demo --xcfg.path=$(ROOT_DIR)/config/$(ENV)
